package com.example.pingze.service;

import com.example.pingze.entity.SentenceUserCollect;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.SentenceUserCollectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class SentenceUserCollectService {
    @Autowired
    private SentenceUserCollectRepository repository;

    public SentenceUserCollect collect(SentenceUserCollect collect) {
        return repository.save(collect);
    }

    public List<SentenceUserCollect> findSentenceUserCollect(SentenceUserCollect collect) {
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(collect.getAccount())){
                    predicates.add(criteriaBuilder.like(root.get("account"), "%" + collect.getAccount() + "%"));
                }
                if(!StringUtils.isEmpty(collect.getSentenceId())){
                    predicates.add(criteriaBuilder.like(root.get("sentenceId"), "%" + collect.getSentenceId() + "%"));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return repository.findAll(specification);
    }

    public void delete(SentenceUserCollect collect) {
        repository.delete(collect);
    }

    public List<SentenceUserCollect> findSentenceUserCollectByAccount(String account) {
        List<SentenceUserCollect> list = repository. findSentenceUserCollectByAccount(account);
        return list;
    }

    public SentenceUserCollect findSentenceUserCollectByAccountAndSentenceId(String account, String sentenceId) {
        return repository.findSentenceUserCollectByAccountAndSentenceId(account, sentenceId);
    }

    public Page getList(SentenceUserCollect sentenceUserCollect, Pageable pageable) {
        Specification<SentenceUserCollect> specification=new Specification<SentenceUserCollect>() {
            @Override
            public Predicate toPredicate(Root<SentenceUserCollect> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(sentenceUserCollect.getId())){
                    predicates.add(criteriaBuilder.equal(root.get("id"),sentenceUserCollect.getId()));
                }
                if(!StringUtils.isEmpty(sentenceUserCollect.getAccount())){
                    predicates.add(criteriaBuilder.equal(root.get("account"),sentenceUserCollect.getAccount()));
                }
                if(!StringUtils.isEmpty(sentenceUserCollect.getSentenceId())){
                    predicates.add(criteriaBuilder.equal(root.get("sentenceId"),sentenceUserCollect.getSentenceId()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = repository.findAll(specification,pageable);
        return page;
    }
    public SentenceUserCollect add(String account, String sentenceId){
        if(StringUtils.isEmpty(account)){
            throw new PingZeException("账号不能为空");
        }
        if(StringUtils.isEmpty(sentenceId)){
            throw new PingZeException("收藏的诗词id不能为空");
        }
        SentenceUserCollect add=new SentenceUserCollect();
        add.setId(UUID.randomUUID().toString());
        add.setAccount(account);
        add.setSentenceId(sentenceId);
        repository.save(add);
        return add;
    }
    public SentenceUserCollect edit(String id,String account,String sentenceId){
        SentenceUserCollect updater= repository.findSentenceUserCollectById(id);
        if(!updater.getAccount().equals(account)){
            updater.setAccount(account);
        }
        if(!updater.getSentenceId().equals(sentenceId)){
            updater.setSentenceId(sentenceId);
        }
        repository.save(updater);
        return  updater;
    }

    public SentenceUserCollect findSentenceUserCollectById(String id) {
        return repository.findSentenceUserCollectById(id);
    }
}
